package com.lin.eduservie.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.commonutils.R;
import com.lin.eduservie.entity.EduCourse;
import com.lin.eduservie.entity.vo.CourseInfoVo;
import com.lin.eduservie.entity.vo.CoursePublishVo;
import com.lin.eduservie.entity.vo.CourseQuery;
import com.lin.eduservie.service.EduCourseService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author linshuwu
 * @since 2021-08-01
 */
@RestController
@RequestMapping("/eduservice/course")
//@CrossOrigin
public class EduCourseController {

    @Autowired
    private EduCourseService CourseService;

    // 课程列表 基本实现
    @GetMapping
    public R getCourseList(){
        List<EduCourse> list = CourseService.list(null);
        return R.ok().data("list",list);
    }

    // 添加课程基本信息的方法
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        // 返回添加之后的课程id，为了后面添加大纲使用
        String id = CourseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId",id);
    }

    // 根据课程id查询课程基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){
        CourseInfoVo courseInfoVo = CourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo",courseInfoVo);
    }

    // 修改课程信息
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
        CourseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    // 根据课程id查询课程确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
        CoursePublishVo coursePublishVo = CourseService.publishCourseInfo(id);
        return R.ok().data("publishCourse",coursePublishVo);
    }

    // 课程最终发布
    // 修改课程状态码status
    @PostMapping("courseEndPublish/{id}")
    public R courseEndPublish(@PathVariable String id){

        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        CourseService.updateById(eduCourse);
        return R.ok();

    }

    // 条件查询带分页的方法
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current,
                                  @PathVariable long limit,
                                  @RequestBody(required = false) CourseQuery courseQuery){
        // 创建page对象
        Page<EduCourse> pageCourse = new Page<>(current,limit);
        // 构建条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        // 多条件组合查询
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        // 判断条件值是否为空，如果不为空拼接条件
        if(!StringUtils.isEmpty(title)){
            // 构建条件
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status",status);
        }

        // 排序
        wrapper.orderByDesc("status");
        // 调用方法实现条件查询分页
        CourseService.page(pageCourse,wrapper);
        long total = pageCourse.getTotal();//总记录数
        List<EduCourse> records = pageCourse.getRecords(); //数据list集合
        return R.ok().data("total",total).data("list",records);
    }

    // 删除课程
    @ApiOperation(value = "删除课程")
    @DeleteMapping("{courseId}")
    public R removeTeacher(@ApiParam(name = "courseId",value = "课程ID",required = true)@PathVariable String courseId){ // id值需要通过路径传播
        CourseService.removeCourse(courseId);
        return R.ok();
//        if(flag){
//            return R.ok();
//        }else {
//            return R.error();
//        }
    }

}

